<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class About extends CI_Controller {
	function About()
	{
		parent::__construct();
		// load language file
		$this->lang->load('i18n');
		$this->load->helper(array('form', 'url'));
		$this->load->config('config');
		$this->load->library('securimage');
	}
	
	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function index()
	{
		// Charge the view inside array
		$data = array();
		$data['content'] = $this->load->view('about-content.phtml', null, true);
		$data['about_selected_class'] = "current";
		
		$this->load->view('mainpage.phtml', $data);
	}
	
	public function send(){
		$this->load->library('form_validation');
		// Set validation rules for feedbacks
		$this->form_validation->set_rules('name', $this->lang->line('lang.about.request.name'), 'required|xss_clean');
		$this->form_validation->set_rules('email', $this->lang->line('lang.about.request.email'), 'required|valid_email|xss_clean');
		$this->form_validation->set_rules('subject', $this->lang->line('lang.about.request.subject'), 'required|xss_clean');
		$this->form_validation->set_rules('content', $this->lang->line('lang.about.request.body'), 'required|xss_clean');
		
		if($this->form_validation->run() == false)
		{	
			$this->session->set_flashdata("errors", validation_errors());
			$this->session->set_flashdata("submitdata", $_POST);
			redirect($this->lang->languageCode().'/about/index', 'location');
		}
		
		if($this->securimage->check($_POST['maxacnhan']) == false){
			$this->session->set_flashdata("errors", "Mã xác nhận không đúng.");
			$this->session->set_flashdata("submitdata", $_POST);
			redirect($this->lang->languageCode().'/about/index', 'location');
		}
		
		if($this->sendEmail($_POST['name'], $_POST['email'], $this->config->item('receiveremail'), $_POST['subject'], $_POST['content'])){
			$this->session->set_flashdata("messages", $this->lang->line('lang.about.request.sent'));
			redirect($this->lang->languageCode().'/about/index', 'location');
		} else {
			$this->session->set_flashdata("errors", $this->lang->line('lang.about.request.fail'));
			redirect($this->lang->languageCode().'/about/index', 'location');
		}
	}
	
	protected function sendEmail($sendername, $sender, $receiver, $subject, $content){
		$config = Array(
		    'protocol' => 'mail',
		    'smtp_host' => '',
		    'smtp_port' => 25,
		    'smtp_user' => '',
		    'smtp_pass' => '',
		);
		$this->load->library('email', $config);
		$this->email->set_newline("\r\n");
		$this->email->from($sender, $sendername);
		$this->email->to($receiver); 
		
		$this->email->subject($subject);
		$this->email->message($content);
			
		return $this->email->send();
	}
	
	function generateCapchar() {
		$img = new Securimage();
		$img->show(); // alternate use: $img->show('/path/to/background.jpg');
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/about.php */